package yxy.game.pm2.module.fortdefeat;

import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.module.RedisLock;

public class ForDefeatDynamicModule extends PlayerModule {

    // bean===========================================================================

    private int degree;//难度等级   规则：困难打通一次+1   每一级提升10%攻防血

    public int getDegree() {
        return degree;
    }

    public void setDegree(int degree) {
        this.degree = degree;
    }


    // redis==========================================================================
    private static String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:FORDEFEATDYNAMIC", playerCode);
    }

    //缓存
    public void set() {
        try {
            String key = key(getPlayerCode());
            setv(key, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    static public ForDefeatDynamicModule get(String playerCode) {
        try {
            String key = key(playerCode);
            ForDefeatDynamicModule module = getv(key);
            //todo 后续缓存加到数据库
            if (module == null) {
                module = create(playerCode);
            }
            return module;
        } catch (RedisException e) {
            e.printStackTrace();
        }
        return null;
    }

    //第一次初始化
    static private ForDefeatDynamicModule create(String playerCode) {
        ForDefeatDynamicModule module = new ForDefeatDynamicModule();
        module.setPlayerCode(playerCode);
        module.setDegree(0);
        module.set();
        return module;
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {

    }

    // lock==========================================================================

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }

    public static RedisLock lock(String playerCode) {
        return getLock(String.format("lock:%s", key(playerCode)));
    }
}
